require 'csv'
class UserController < ApplicationController
	layout 'standard'
	
	before_filter :authorizeAdmin
    
	
	
	def search
		
		@positions = Position.find(:all, :order => "NAME").map {|u| [u.name, u.id] }		
		@systemRoles = SystemRole.find(:all, :order => "NAME").map {|u| [u.name, u.id] }		
		@years = get_periods 10
		respond_to do |format|
			format.html 
			format.xml { render :xml => @users }
		end		
	end
	
	def index
		conditions=""
		if(params[:login_check]) 	
			conditions<<"LOGIN = '#{params[:login]}'"
		end
		if(params[:email_check])
			conditions<<" AND " unless conditions.length==0			
			conditions<<"EMAIL='#{params[:email]}'"
		end
		if(params[:pesel_check])
			conditions<<" AND " unless conditions.length==0			
			conditions<<"PESEL='#{params[:pesel]}'"
		end
		if(params[:role_check])
			conditions<<" AND " unless conditions.length==0			
			conditions<<"ROLE_ID='#{params[:role][:roleID]}'"
		end
		if(params[:position_check])
			conditions<<" AND " unless conditions.length==0			
			conditions<<"POSITION_ID='#{params[:user_positionID]}'"
			if(params[:user_positionID].to_i==Position.get_student_id) && params[:period_check]
				conditions<<"AND STUDY_PERIOD='#{params[:user_study_period]}'"	
			end
		end
		@users = User.find(:all, :order => :login,:conditions=>conditions)
		
		respond_to do |format|
			format.html # index.html.erb
			format.xml { render :xml => @users }
		end
	end
	
	
	def show
		@user = User.find(params[:id])
		respond_to do |format|
			format.html # show.html.erb
			format.xml { render :xml => @user }
		end
	end
	
	def new
		
		
		@user = User.new
		@positions = Position.find(:all, :order => "NAME").map {|u| [u.name, u.id] }		
		@systemRoles = SystemRole.find(:all, :order => "NAME").map {|u| [u.name, u.id] }			
		@years = get_periods 10
	
		respond_to do |format|
			format.html # new.html.erb
			format.xml { render :xml => @user }
		end
	end
	
	def create
			params[:user][:activated]="n"
		@user = User.new(params[:user])
	
		respond_to do |format|
			if @user.save
				flash[:notice] = "User #{@user.login} was successfully created."
				format.html { redirect_to(:action=>:index) }
				format.xml  { render :xml => @user, :status => :created,
                             :location => @user }
			else
				@positions = Position.find(:all, :order => "NAME").map {|u| [u.name, u.id] }		
				@systemRoles = SystemRole.find(:all, :order => "NAME").map {|u| [u.name, u.id] }			
				@years = get_periods 10

				format.html { render :action => "new" }
				format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
			end
		end
	end
	
	
	def edit
		
		@positions = Position.find(:all, :order => "NAME").map {|u| [u.name, u.id] }		
		@systemRoles = SystemRole.find(:all, :order => "NAME").map {|u| [u.name, u.id] }			
		@user = User.find(params[:id])
	
    	respond_to do |format|
			format.html 
			format.xml { render :xml => @user }
		end
		
	end
	

	def update
		@user = User.find(params[:id])
		respond_to do |format|
			if @user.update_attributes(params[:user])
				flash[:notice] = "User #{@user.name} was successfully updated."
				format.html { redirect_to(:action=>:index) }
				format.xml { head :ok }
				else
				format.html { render :action => "edit" }
				format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
				end
		end
	end
	
	def destroy
		@user = User.find(params[:id])
		@user.destroy
		respond_to do |format|
			format.html { redirect_to(users_url) }
			format.xml { head :ok }
		end
	
	end 

	
	def get_periods count
		periods=Hash.new
		curr_year=Time.now.year
		0.upto(count) do |x|
			label="#{curr_year-x}/#{curr_year-x+5}"
			periods[label]=label
		end
		periods
	end
	
	
	def import_users
		@positions = Position.find(:all, :order => "NAME").map {|u| [u.name, u.id] }		
		@systemRoles = SystemRole.find(:all, :order => "NAME").map {|u| [u.name, u.id] }			
		@years = get_periods 10
	
		@file=UploadedFile.new
		respond_to do |format|
			format.html 
			format.xml { head :ok }
		end

	end
	
	def parse_csv	
#			@file=UploadedFile.new
#			UploadedFile.save(params[:file][:upload])
		student_id=Position.get_student_id
		@parsed_file=CSV::Reader.parse(params[:file][:upload].read)
	    n=0
		@parsed_file.each  do |row|
			 user=User.new
			 user.login=row[0].data unless row[0]==nil
			 user.PASSWORD=row[1].data unless row[1]==nil
			 if row[2]!=nil  
			 	user.email=row[2].data
		 	 end
		 	 if row[3]!=nil  
		     	user.pesel=row[3].data
		     end
		     if row[4]!=nil    
		     		role=SystemRole.find_by_name(row[4].data)
		     		if role!=nil
		     			user.role_id=role.id
	     			end
	     		else
	     	
     	 	 end
     	 	 if user.role_id==nil
     	 	  		user.role_id=params[:role][:roleID] 	
 			 end
     	 	 if row[5]!=nil  
		     		position=Position.find_by_name(row[5].data)
		     		if position!=nil
		     			user.position_id=position.id
	     			end
	    	 end
	    	 if user.position_id==nil
	    	  	 	user.position_id=params[:user_positionID] 	
    	 	end
			 if user.position_id==student_id
		     		if row[6]!=nil
		     			user.study_period=row[6].data
	     			else
	     				user.study_period=params[:user_study_period]
     				end
	     	 end
		    	
		     user.expiry_date=Time.now
		     user.activated='y'
			 if user.save
		    	 n=n+1
		     end		     	 			
		end
		flash[:notice]="Plik CSV zaimportowany - liczba dodanych użytkowników: #{n}"
		respond_to do |format|
			format.html { redirect_to(:controller=>"user",:action=>"import_users") }
			format.xml { head :ok }
		end
	
	end

end
